package com.sqfw.project.wpReport.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.system.domain.SysDept;
import com.sqfw.project.system.mapper.SysDeptMapper;
import com.sqfw.project.wp.domain.WpHandleLog;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.mapper.WpHandleLogMapper;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import com.sqfw.project.wp.utils.ListUtils;
import com.sqfw.project.wpReport.domain.dto.WpInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wpReport.domain.WpInfoTransfer;
import com.sqfw.project.wpReport.service.IWpInfoTransferService;

/**
 * 承办单位多次处理Service业务层处理
 *
 * @author lx
 * @date 2023-07-15
 */
@Service
public class WpInfoTransferServiceImpl implements IWpInfoTransferService {
    @Autowired
    private WpInfoMapper wpInfoMapper;

    @Autowired
    private WpHandleLogMapper wpHandleLogMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public List<WpInfoTransfer> getWpInfoListTransfer(List<String> wpids) {

        WpInfoDTO dto = new WpInfoDTO();
        dto.setWpids(wpids);
        List<WpInfo> wpInfos = wpInfoMapper.getWpInfoListByDTO(dto);
        List<WpInfoTransfer> wpInfoTransfers = new ArrayList<>();
        for (WpInfo wpInfo : wpInfos) {
            WpInfoTransfer wpInfoTransferCon = new WpInfoTransfer();
            wpInfoTransferCon.setIndex(wpInfoTransfers.size()+1);
            wpInfoTransferCon.setScid(wpInfo.getScid());
            wpInfoTransferCon.setWpid(wpInfo.getWpid());
            wpInfoTransferCon.setDispatchTime(wpInfo.getStarttime());
            wpInfoTransferCon.setTsWpid(wpInfo.getTsWpid());
            WpInfoTransfer transfer = new WpInfoTransfer();
            transfer.setWpid(wpInfo.getWpid());
            List<WpHandleLog> wpHandleLogs = wpHandleLogMapper.selectWpHandleLogListByDeptIds(transfer);
            Set<String> deptIds = wpHandleLogs.stream()
                    .map(WpHandleLog::getNextDepartment1)
                    .collect(Collectors.toSet());

            StringBuilder stringBuilder = new StringBuilder();

            for (String deptId : deptIds) {
                SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(deptId));
                if (sysDept != null) {
                    stringBuilder.append(sysDept.getDeptName()).append("/");
                }
            }

            String deptName = stringBuilder.toString();
            if (StringUtils.isNotEmpty(deptName)) {
                String substring = deptName.substring(0, deptName.length() - 1);
                wpInfoTransferCon.setDeptName(substring);
            }

            WpHandleLog wpHandleLog = wpHandleLogs.stream().max(Comparator.comparing(WpHandleLog::getUpdateTime)).orElse(null);
            if (wpHandleLog!=null){
                wpInfoTransferCon.setLastmessage(wpHandleLog.getMessage());
            }else {
                wpInfoTransferCon.setLastmessage(wpInfo.getLastmessage());
            }
            wpInfoTransfers.add(wpInfoTransferCon);
        }
        return wpInfoTransfers;
    }

    @Override
    public List<String> getWpInfoListTransferList(WpInfoTransfer wpInfoTransfer) {
        List<String> wpidsCon = new ArrayList<>();
        List<WpInfo> wpInfos = wpInfoMapper.getWpInfoListTransferList(wpInfoTransfer);
        List<String> wpidsRecover = wpInfos.stream().filter(info -> "1".equals(info.getRecoverState())).map(WpInfo::getWpid).collect(Collectors.toList());
        if (wpInfos.size() == 0) {
            return wpidsCon;
        }
        List<String> wpids = wpInfos.stream().map(WpInfo::getWpid).collect(Collectors.toList());

        List<String> mainUnit = wpInfoTransfer.getMainUnit();


        if (StringUtils.isNotEmpty(mainUnit)) {
            List<String> deptIds = new ArrayList<>();
            for (String deptId : mainUnit) {
                String[] split = deptId.split(",");
                deptIds.add(split[split.length - 1]);
            }
            wpInfoTransfer.setDeptIds(deptIds);
        }

        List<List<String>> wpidsList = ListUtils.split(wpids, 1000);
        for (List<String> strings : wpidsList) {
            //查询工单中承办单位处理轨迹记录
            List<WpHandleLog> handleLogs = wpHandleLogMapper.selectWpHandleLogListByDeptIdsAndWpids(strings, wpInfoTransfer.getDeptIds());
            Map<String, List<WpHandleLog>> wpHandleLogMap = handleLogs.stream().collect(Collectors.groupingBy(WpHandleLog::getWpid));
            for (Map.Entry<String, List<WpHandleLog>> stringListEntry : wpHandleLogMap.entrySet()) {
                List<WpHandleLog> value = stringListEntry.getValue();

                if (wpidsRecover.size()>0&&wpidsRecover.contains(stringListEntry.getKey())){
                    //审核人撤回过工单，判断是否同一个工单多次承办单位处理
                    if (value.size() >= 3) {
                        wpidsCon.add(stringListEntry.getKey());
                    }
                }else {
                    //审核人没有撤回过工单判断是否同一个工单多次承办单位处理
                    if (value.size() >= 2) {
                        wpidsCon.add(stringListEntry.getKey());
                    }
                }
            }
        }
        return wpidsCon;
    }

    @Override
    public WpInfoTransfer getWpInfoListTransferByWpid(String wpid) {

        WpInfo wpInfo = wpInfoMapper.selectWpInfoByWpId(wpid);


        WpInfoTransfer wpInfoTransferCon = new WpInfoTransfer();
        wpInfoTransferCon.setScid(wpInfo.getScid());
        wpInfoTransferCon.setWpid(wpInfo.getWpid());
        wpInfoTransferCon.setTsWpid(wpInfo.getTsWpid());
        wpInfoTransferCon.setDispatchTime(wpInfo.getStarttime());

        WpInfoTransfer transfer = new WpInfoTransfer();
        transfer.setWpid(wpInfo.getWpid());
        List<WpHandleLog> wpHandleLogs = wpHandleLogMapper.selectWpHandleLogListByDeptIds(transfer);
        Set<String> deptIds = wpHandleLogs.stream()
                .map(WpHandleLog::getNextDepartment1)
                .collect(Collectors.toSet());

        StringBuilder stringBuilder = new StringBuilder();

        for (String deptId : deptIds) {
            SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(deptId));
            if (sysDept != null) {
                stringBuilder.append(sysDept.getDeptName()).append("/");
            }
        }

        String deptName = stringBuilder.toString();
        if (StringUtils.isNotEmpty(deptName)) {
            String substring = deptName.substring(0, deptName.length() - 1);
            wpInfoTransferCon.setDeptName(substring);
        }


        wpInfoTransferCon.setLastmessage(wpInfo.getLastmessage());


        return wpInfoTransferCon;
    }
}
